package com.example.memorypoint.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.AlertDialog;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.memorypoint.DateBase.DiaryDatabase;
import com.example.memorypoint.R;
import com.example.memorypoint.adapter.MsgAdapter;
import com.example.memorypoint.entity.Msg;
import com.example.memorypoint.mvp.aichat.AiChatContract;
import com.example.memorypoint.mvp.aichat.AiChatPresenterImpl;

import java.util.ArrayList;
import java.util.List;

public class AiChatActivity extends AppCompatActivity implements AiChatContract.AiChatView {

    private List<Msg> msgList = new ArrayList<>();
    private RecyclerView msgRecyclerView;
    private EditText inputText;
    private ImageView send;
    private MsgAdapter adapter;
    private DiaryDatabase db;
    private AiChatContract.AiChatPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ai_chat);

        msgRecyclerView = findViewById(R.id.msg_recycler_view);
        inputText = findViewById(R.id.input_text);
        send = findViewById(R.id.send);
        db = new DiaryDatabase(this);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        adapter = new MsgAdapter(msgList);
        msgRecyclerView.setLayoutManager(layoutManager);
        msgRecyclerView.setAdapter(adapter);

        // 创建 Presenter，并初始化聊天
        presenter = new AiChatPresenterImpl(this, db);
        presenter.initChat();

        // 发送按钮点击事件
        send.setOnClickListener(v -> {
            String content = inputText.getText().toString().trim();
            if (!content.isEmpty()) {
                presenter.sendMessage(content);
                inputText.setText("");
            }
        });
    }

    @Override
    public void addMessage(Msg msg) {
        msgList.add(msg);
        adapter.notifyItemInserted(msgList.size() - 1);
        msgRecyclerView.scrollToPosition(msgList.size() - 1);
    }

    @Override
    public void updateMessageAt(int index, Msg msg) {
        if (index >= 0 && index < msgList.size()) {
            msgList.set(index, msg);
            adapter.notifyItemChanged(index);
            msgRecyclerView.scrollToPosition(msgList.size() - 1);
        }
    }

    @Override
    public void showError(String error) {
        // 可以使用 Toast 或其他方式显示错误信息
         Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public int getMessageListSize() {
        return msgList.size();
    }

    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(this)
                .setMessage("聊天记录将不会保存，你确定要返回吗？")
                .setCancelable(false)
                .setPositiveButton("确定", (dialog, which) -> AiChatActivity.super.onBackPressed())
                .setNegativeButton("取消", null)
                .show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detachView();
        }
    }
}
